<template>
  <div>
    <my-header background="red" title="购物车案例"></my-header>
    <div class="main">
      <my-goods v-for="obj in list" :key="obj.id" :gObj="obj"></my-goods>
    </div>
    <my-footer @changeAll="allFn" :list="list"></my-footer>
  </div>
</template>

<script>
import MyHeader from "./components/MyHeader.vue";
import MyFooter from "./components/MyFooter.vue";
import MyGoods from "./components/MyGoods.vue";

export default {
  components: { MyHeader, MyFooter, MyGoods },
  data() {
    return {
      list: [],
    };
  },
  created() {
    // 不必再自己引入axios变量，而是直接使用全局属性$axios
    this.$axios({
      url: "/api/cart",
    }).then((res) => {
      this.list = res.data.list;
    });
  },
  methods: {
    allFn(bool) {
      this.list.forEach((obj) => (obj.goods_state = bool));
    },
  },
};
</script>

<style>
.main {
  margin-top: 45px;
  margin-bottom: 45px;
}
</style>